var logionWin = {
    init: function(){
        if (!this.logForm) {
            this.logForm = new CQCIS.form.DWRForm({
                labelWidth: 50,
                resizable: false,
                buttons: [new CQCIS.Button({
                    text: '登　　录',
                    handler: this.submit,
                    KeyMap: new Ext.KeyMap(document, {
                        key: 13,
                        scope: this,
                        fn: this.submit
                    })
                }), new CQCIS.Button({
                    text: '返回首页',
                    handler: this.close,
                    KeyMap: new Ext.KeyMap(document, {
                        key: 27,
                        scope: this,
                        fn: this.close
                    })
                })],
                metas: [{
                    field: 'logincode',
                    desc: '用户名',
                    anchor: '90%',
                    obj: {
                        allowBlank: false,
                        enableKeyEvents: true,
                        validationEvent: 'blur',
                        listeners: {
                            'keyup': function(o, evt){
                                o.setValue(o.getValue().toUpperCase());
                            }
                        }
                    }
                }, {
                    field: 'password',
                    desc: '密　码',
                    anchor: '90%',
                    xtype: 'pwd',
                    obj: {
                        allowBlank: false
                    }
                }]
            });
        }
        if (!this.logWin) {
            this.logWin = new CQCIS.form.WinForm({
                title: '用户登录',
                width: 270,
                minWidth: 270,
                height: 130,
                autoHeight: false,
                buttons: [],
                closable: false,
                draggable: false,
                resizable: false,
                keys: true,
                items: this.logForm
            });
        }
        this.logWin.load();
    },
    submit: function(){
        if (logionWin.logForm.buttons[0].disabled) {
            return;
        }
        var form = logionWin.logForm.form;
        if (form.isValid()) {
            logionWin.logForm.buttons[0].disable();
            logionWin.logForm.buttons[1].disable();
            authDwr.login(form.findField('logincode').getValue(), form.findField('password').getValue(), function(lv){
                if (lv.loginid) {
                    if (CURRENTUSER && CURRENTUSER.userName == lv.username) {
                        logionWin.logWin.hide();
                        if (west.getLayout().activeItem && west.items.indexOf(west.getLayout().activeItem) > 0) {
                            menuTree.reload(west.items.indexOf(west.getLayout().activeItem));
                        }
                        center.getActiveTab().setSrc(center.getActiveTab().iframe.dom.src);
                    } else if (CURRENTUSER && CURRENTUSER.userName != lv.username) {
                        top.location.reload();
                    } else {
                        logionWin.logWin.hide();
                        CURRENTUSER = lv;
                        mainFrame.init();
                    }
                } else {
                    CQCIS.msg(lv.message);
                }
                logionWin.logForm.buttons[0].enable();
                logionWin.logForm.buttons[1].enable();
            });
        } else {
            CQCIS.msg('用户名和密码不能为空！');
        }
    },
    close: function(){
        if (logionWin.logForm.buttons[0].disabled) {
            return;
        }
        top.location.replace(CQCIS.ctxPath + 'index.html');
    }
};

function uLogoff(){
    CQCIS.confirm("您真的要退出系统吗?", function(pm){
        if (pm == 'yes') {
            authDwr.logoff(function(){
                location.replace(CQCIS.ctxPath + "index.html");
            });
        }
    });
}
